/**
 * Copyright 2012 University Of Illinois
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/**
 * @file sidebar-option-menu.js
 * 
 * Contains the utility functions to update preferences and dialog event handlers to support preference.xul dialog
 */

/* ---------------------------------------------------------------- */
/*                  Support Options Menus in Sidebar                */
/* ---------------------------------------------------------------- */

/**
 * @function OAA_AE_SIDEBAR.setOptionMenusFromPreferences
 * 
 * @desc Updates menu options based on current preferences
 */
 
OAA_AE_SIDEBAR.initRulesetOptions = function (id) {

  if (typeof id !== 'string') id = "";

  function addMenuItem(ruleset, selected) {
  
    mi = document.createElement('menuitem');
    
    mi.setAttribute('id', ruleset.ruleset_id);
    mi.setAttribute('type', 'radio');
    mi.setAttribute('group', 'ruleset');
    mi.setAttribute('label', ruleset.ruleset_title + " (verison " + ruleset.ruleset_version + ")");
    if (selected) mi.setAttribute('checked', 'true');
    
    popup.appendChild(mi);  
    
  }
  
  var mi;
  var popup = window.document.getElementById("ID_MENUPOPUP_SET_RULESET");
  OAA_AE_SIDEBAR.popup_ruleset = popup;

  var rulesets = OpenAjax.a11y.all_rulesets.getAllRulesets();
  var rulesets_len = rulesets.length;

  for (var i = 0; i < rulesets_len; i++) addMenuItem(rulesets[i], (rulesets[i] === id));
  
  mi = document.createElement('menuseparator');
  mi.setAttribute('id', 'ID_MENU_SEPARATOR_END_OF_RULESETS');
  popup.appendChild(mi); 

  mi = document.createElement('menuitem');
  mi.setAttribute('id', 'ID_MENUITEM_SET_RECOMMENDED_RULES');
  mi.setAttribute('label', 'Include Recommended Rules (e.g. Warning results)');
  mi.setAttribute('type', 'checkbox');
  popup.appendChild(mi); 
  OAA_AE_SIDEBAR.enable_recommended = mi;

};


/**
 * @function OAA_AE_SIDEBAR.setRulesetMenuFromPreferences
 * 
 * @desc Updates menu options based on current preferences
 */
 
OAA_AE_SIDEBAR.setRulesetMenuFromPreferences = function () {

  var AP = OAA_WEB_ACCESSIBILITY_PREF;
  var p  = AP.preferences;

  var popup = OAA_AE_SIDEBAR.popup_ruleset;
  
  var child = popup.firstChild;
  var i = 1;

  OAA_AE_SIDEBAR.logger.debug("In set menu options: " + popup);

  while (child && (child.id !== 'ID_MENU_SEPARATOR_END_OF_RULESETS')) {

//    OAA_AE_SIDEBAR.logger.debug( i++ + " child ID: " + child.id);

    if (child.id === p.ruleset_id) {
      OAA_AE_SIDEBAR.last_ruleset_id = p.ruleset_id;
      child.setAttribute('checked', 'true');
      break;
    }
  
    child = child.nextSibling;
  }
  
  var node = OAA_AE_SIDEBAR.enable_recommended;
  
  OAA_AE_SIDEBAR.last_recommended = p.recommended_rules_enabled;
  if (node) node.setAttribute('checked', p.recommended_rules_enabled);

};
        


/**
 * @function OAA_AE_SIDEBAR.setPreferencesFromRulesetMenu
 *
 * @desc Set preferences from option menus
 */

OAA_AE_SIDEBAR.setPreferencesFromRulesetMenu = function () {

  var p  = OAA_WEB_ACCESSIBILITY_PREF.preferences;
  var update = false;

  var popup = OAA_AE_SIDEBAR.popup_ruleset;

  OAA_AE_SIDEBAR.logger.debug("In set preferences from ruleset menu options: " + popup);
  
  var child = popup.firstChild;
  var i = 1;


  while (child && (child.id !== 'ID_MENU_SEPARATOR_END_OF_RULESETS')) {

    if (child.getAttribute('checked') === 'true') {
      p.ruleset_id = child.id;
      update = (OAA_AE_SIDEBAR.last_ruleset_id !== child.id);
      OAA_AE_SIDEBAR.logger.debug( "checked menu item: " + i + " child ID: " + child.id);
      break;
    }
  
    child = child.nextSibling;
  }
  
  var node = OAA_AE_SIDEBAR.enable_recommended;
  if (node) { 
    var checked = (node.getAttribute('checked') === 'true');
    update = update || (checked != OAA_AE_SIDEBAR.last_recommended);
    p.recommended_rules_enabled = checked;
  }  

  OAA_WEB_ACCESSIBILITY_PREF.util.setPreferences(update);
  
};


/**
 * @function OAA_AE_SIDEBAR.setFilterMenuFromPreferences
 * 
 * @desc Updates menu options based on current preferences
 */
 
OAA_AE_SIDEBAR.setFilterMenuFromPreferences = function () {

  var p  = OAA_WEB_ACCESSIBILITY_PREF.preferences;
  var doc = window.document;  
  var mi;
  

  // Radio group for WCAG 2.0 level

  OAA_AE_SIDEBAR.last_evaluation_levels = p.evaluation_levels;

  switch (p.evaluation_levels) {
  
  case OpenAjax.a11y.EVALUATION_LEVELS.A:
    mi = doc.getElementById('ID_MENUITEM_WCAG10_LEVEL_A');  
    mi.setAttribute('checked', 'true');
    break;
    
  case OpenAjax.a11y.EVALUATION_LEVELS.A_AA:
    mi = doc.getElementById('ID_MENUITEM_WCAG10_LEVEL_AA');  
    mi.setAttribute('checked', 'true');
    break;
    
  case OpenAjax.a11y.EVALUATION_LEVELS.A_AA_AAA:
    mi = doc.getElementById('ID_MENUITEM_WCAG10_LEVEL_AAA');  
    mi.setAttribute('checked', 'true');
    break;
    
  default:
    break;
    
  }

  // Other evaluation options

  mi = document.getElementById("ID_MENUITEM_SET_TABLES_FOR_LAYOUT");
  if (mi) mi.setAttribute('checked', p.layout_tables);
  OAA_AE_SIDEBAR.last_layout_tables = p.layout_tables;

  mi = document.getElementById("ID_MENUITEM_SET_TEST_BROKEN_LINKS");
  if (mi) mi.setAttribute('checked', p.broken_links);
  OAA_AE_SIDEBAR.last_broken_links = p.broken_links;

  // Evaluation Results Filter

  mi  = document.getElementById("ID_MENUITEM_SHOW_ELEMENT_MANUAL_CHECKS");
  if (mi) mi.setAttribute('checked', p.show_results_element_manual_checks);
  OAA_AE_SIDEBAR.last_show_results_element_manual_checks = p.show_results_element_manual_checks;

};
        


/**
 * @function OAA_AE_SIDEBAR.setPreferencesFromFilterMenu
 *
 * @desc Set preferences from option menus
 */

OAA_AE_SIDEBAR.setPreferencesFromFilterMenu = function () {

  var p  = OAA_WEB_ACCESSIBILITY_PREF.preferences;
  var doc = window.document;  
  var mi;
  var update = false;
  var checked;
  
  // Radio group for Evaluation level

  OAA_AE_SIDEBAR.last_evaluation_levels = p.evaluation_levels;

  mi = doc.getElementById('ID_MENUITEM_WCAG10_LEVEL_A');  
  if (mi.getAttribute('checked') === 'true') {
    p.evaluation_levels = OpenAjax.a11y.EVALUATION_LEVELS.A;
    update = update || (OAA_AE_SIDEBAR.last_evaluation_levels !== OpenAjax.a11y.EVALUATION_LEVELS.A);
  } 
 
  mi = doc.getElementById('ID_MENUITEM_WCAG10_LEVEL_AA');  
  if (mi.getAttribute('checked') === 'true') {
    p.evaluation_levels = OpenAjax.a11y.EVALUATION_LEVELS.A_AA;
    update = update || (OAA_AE_SIDEBAR.last_evaluation_levels !== OpenAjax.a11y.EVALUATION_LEVELS.A_AA);
  } 

  mi = doc.getElementById('ID_MENUITEM_WCAG10_LEVEL_AAA');  
  if (mi.getAttribute('checked') === 'true') {
    p.evaluation_levels = OpenAjax.a11y.EVALUATION_LEVELS.A_AA_AAA;
    update = update || (OAA_AE_SIDEBAR.last_evaluation_levels !== OpenAjax.a11y.EVALUATION_LEVELS.A_AA_AAA);
  } 
    
  // Other evaluation options

  mi = document.getElementById("ID_MENUITEM_SET_TABLES_FOR_LAYOUT");
  p.layout_tables = mi.getAttribute('checked') === 'true';
  update = update || (OAA_AE_SIDEBAR.last_layout_tables !== p.layout_tables);

  mi = document.getElementById("ID_MENUITEM_SET_TEST_BROKEN_LINKS");
  p.broken_links = mi.getAttribute('checked') === 'true';
  update = update || (OAA_AE_SIDEBAR.last_broken_links !== p.broken_links);

  // Evaluation Results Filter

  mi  = document.getElementById("ID_MENUITEM_SHOW_ELEMENT_MANUAL_CHECKS");
  p.show_results_element_manual_checks = mi.getAttribute('checked') === 'true';
  update = update || (OAA_AE_SIDEBAR.last_show_results_element_manual_checks !== p.show_results_element_manual_checks);

  OAA_WEB_ACCESSIBILITY_PREF.util.setPreferences(update);
  
};




